package com.yfbao.horizon.common;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.time.Instant;
import java.util.TimeZone;


public class JsonUtil {
    private static ObjectMapper objectMapper = new ObjectMapper();

    static {
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        JavaTimeModule javaTimeModule = new JavaTimeModule();
        objectMapper.registerModule(javaTimeModule);

        // 关键配置
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
        objectMapper.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

    static class TestEntity {
        private Instant createTime;
        private String name;

        public TestEntity() {}

        public TestEntity(Instant createTime, String name) {
            this.createTime = createTime;
            this.name = name;
        }

        // getters and setters
        public Instant getCreateTime() { return createTime; }
        public void setCreateTime(Instant createTime) { this.createTime = createTime; }
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
    }

    public static void main(String[] args) throws Exception {
        // 测试Instant序列化
        TestEntity entity = new TestEntity(Instant.now(), "test");

        String json = objectMapper.writeValueAsString(entity);
        System.out.println("序列化结果: " + json);

        TestEntity deserialized = objectMapper.readValue(json, TestEntity.class);
        System.out.println("反序列化结果: " + deserialized.getCreateTime());

        // 验证是否相等
        System.out.println("时间是否一致: " + entity.getCreateTime().equals(deserialized.getCreateTime()));
    }
}
